<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>modbus</title>
		<script type="text/javascript" src="js/xhr.js"></script>
		<script type="text/javascript">
		var open_air={
			"type":2,
			"data":{
				"key":103,
				"val":1
				}
		    }
			var close_air={
			"type":2,
			"data":{
				"key":103,
				"val":0
				}
		    }
			var open_light={
			"type":2,
			"data":{
				"key":105,
				"val":1
				}
		    }
			var close_light={
			"type":2,
			"data":{
				"key":105,
				"val":0
				}
		    }
			function fun1(){
				XHR.post('cgi-bin/web.cgi', JSON.stringify(open_air));
			}
			function fun2(){
				XHR.post('cgi-bin/web.cgi', JSON.stringify(close_air));
			}
			function fun3(){
				XHR.post('cgi-bin/web.cgi', JSON.stringify(open_light));
			}
			function fun4(){
				XHR.post('cgi-bin/web.cgi', JSON.stringify(close_light));
			}
			function init() {
				refresh();
			}

			function refresh() {
				//这里是请求协议，需要根据双方约定来
				var post_req = {
					"type": 1,
					"limit": "all"
				};

				XHR.post('cgi-bin/web.cgi', JSON.stringify(post_req),
	function(x, info) {
		console.log(info);
		if (info.result != 0) {
			alert("get data err");
		}
		for (var i=0; i<info.data.length; i++) {
			console.log('data = ', info.data[i].val)
			
			if(info.data[i].key == 101)
			{
				document.getElementById('temp').value = info.data[i].val;
			}
			else if(info.data[i].key == 102)
			{
				document.getElementById('hum').value = info.data[i].val;
			}
			else if(info.data[i].key == 104)
			{
				document.getElementById('air_tmp').value = info.data[i].val;
			}
			
		}
	}
);
			}
		</script>
	</head>
	<body onload="init()">
		<label for="溫度">溫度:</label>
		<input type="text" name="mptemp" value="admin" id="temp" size="25">
        <br>
        
		<label for="湿度">  湿度    :</label>
		<input type="hum" name="mphum" value="admin" id="hum" size="25">
        <br>
        
		<label for="空调温度">  空调温度    :</label>
		<input type="air_tmp" name="mpair_tmp" value="admin" id="air_tmp" size="25">
        <br>
        
		<label for="空调开关">   空调开关   :</label>
		<label for="air_on">ON</label>
       <input type="radio" name="air"  id="air_on" onclick="fun1()">
       <label for="air_off">OFF</label>
       <input type="radio" name="air" id="air_off"  checked="checked" onclick="fun2()">
        <br>

        <label for="灯控开关">  灯控开关  :</label>
		<label for="led_on">ON</label>
        <input type="radio" name="led"  id="led_on" onclick="fun3()">
        <label for="led_off">OFF</label>
        <input type="radio" name="led" id="led_off"  checked="checked" onclick="fun4()">	
        <br>
		<input type="button" name="refresh" onclick="init()" value="刷新">
		<br>
		<a href="temp_index.html">主页</a>
	</body>
</html>
